package com.ruoyi.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
        import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sales.domain.ZcStoreInfo;
import com.ruoyi.sales.domain.ZcStoreProduct;
import com.ruoyi.sales.mapper.ZcStoreInfoMapper;
import com.ruoyi.sales.mapper.ZcStoreProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.mapper.ZcStoreEnterMapper;
import com.ruoyi.sales.domain.ZcStoreEnter;
import com.ruoyi.sales.service.IZcStoreEnterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库Service业务层处理
 *
 * @author woo
 * @date 2024-04-11
 */
@Service
public class ZcStoreEnterServiceImpl extends ServiceImpl<ZcStoreEnterMapper, ZcStoreEnter> implements IZcStoreEnterService {
    @Autowired
    private ZcStoreEnterMapper zcStoreEnterMapper;
    @Autowired
    private ZcStoreProductMapper zcStoreProductMapper;
    @Autowired
    private ZcStoreInfoMapper zcStoreInfoMapper;

    /**
     * 查询入库
     *
     * @param enterId 入库主键
     * @return 入库
     */
    @Override
    public ZcStoreEnter selectZcStoreEnterByEnterId(Long enterId) {
        return zcStoreEnterMapper.selectZcStoreEnterByEnterId(enterId);
    }

    /**
     * 查询入库列表
     *
     * @param zcStoreEnter 入库
     * @return 入库
     */
    @Override
    public List<ZcStoreEnter> selectZcStoreEnterList(ZcStoreEnter zcStoreEnter) {
        return zcStoreEnterMapper.selectZcStoreEnterList(zcStoreEnter);
    }

    /**
     * 新增入库
     *
     * @param zcStoreEnter 入库
     * @return 结果
     */
    @Override
    public int insertZcStoreEnter(ZcStoreEnter zcStoreEnter) {
                zcStoreEnter.setCreateTime(DateUtils.getNowDate());
                zcStoreEnter.setDel((long) 0);
                zcStoreEnter.setStaffId(SecurityUtils.getUserId());
                zcStoreEnter.setEnterTime(zcStoreEnter.getCreateTime());
            return zcStoreEnterMapper.insertZcStoreEnter(zcStoreEnter);
    }

    /**
     * 修改入库
     *
     * @param zcStoreEnter 入库
     * @return 结果
     */
    @Override
    public int updateZcStoreEnter(ZcStoreEnter zcStoreEnter) {
                zcStoreEnter.setUpdateTime(DateUtils.getNowDate());
        return zcStoreEnterMapper.updateZcStoreEnter(zcStoreEnter);
    }

    /**
     * 批量删除入库
     *
     * @param enterIds 需要删除的入库主键
     * @return 结果
     */
    @Override
    public int deleteZcStoreEnterByEnterIds(Long[] enterIds) {
        return zcStoreEnterMapper.deleteZcStoreEnterByEnterIds(enterIds);
    }

    /**
     * 删除入库信息
     *
     * @param enterId 入库主键
     * @return 结果
     */
    @Override
    public int deleteZcStoreEnterByEnterId(Long enterId) {
        return zcStoreEnterMapper.deleteZcStoreEnterByEnterId(enterId);
    }

    @Override
    @Transactional
    public int checkZcStoreEnter(ZcStoreEnter zcStoreEnter) {
        QueryWrapper<ZcStoreEnter> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("enter_status",0);
        List<ZcStoreEnter> zcStoreEnters = zcStoreEnterMapper.selectList(qw);
        ZcStoreProduct storeProduct = new ZcStoreProduct();
        for (ZcStoreEnter enter : zcStoreEnters) {
            if (enter.getEnterId().equals(zcStoreEnter.getEnterId())){
                enter.setEnterStatus(zcStoreEnter.getEnterStatus());
                enter.setRemark(zcStoreEnter.getRemark());
                enter.setStoreId(zcStoreEnter.getStoreId());
                zcStoreEnterMapper.updateZcStoreEnter(enter);

//                ZcStoreInfo zcStoreInfo = zcStoreInfoMapper.selectZcStoreInfoByStoreId(zcStoreEnter.getStoreId());
//                zcStoreInfo.setTrueCount(Long.valueOf(zcStoreEnter.getMeters()));
//                zcStoreInfoMapper.updateZcStoreInfo(zcStoreInfo);

                //成品仓库
                storeProduct.setStoreId(zcStoreEnter.getStoreId());
                storeProduct.setEnterId(zcStoreEnter.getEnterId());
                storeProduct.setRemark(zcStoreEnter.getRemark());
                storeProduct.setCreateTime(DateUtils.getNowDate());
                storeProduct.setDel((long) 0);
                zcStoreProductMapper.insertZcStoreProduct(storeProduct);
            }
        }
        return 1;
    }

    @Override
    public List<ZcStoreEnter> checkAllStatus() {
        QueryWrapper<ZcStoreEnter> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("enter_status",0);
        List<ZcStoreEnter> zcStoreEnters = zcStoreEnterMapper.selectList(qw);
        return zcStoreEnters;
    }

}